package com.chat.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chat.admin.domain.DTO.ReleaseVersionDTO;
import com.chat.admin.domain.DTO.VersionDTO;
import com.chat.admin.domain.VO.VersionVO;
import com.chat.admin.mapper.VersionMapper;
import com.chat.admin.service.IFileMapService;
import com.chat.admin.service.IVersionService;
import com.chat.common.entity.Admin;
import com.chat.common.entity.Version;
import com.chat.common.form.ListForm;
import com.chat.common.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author weikjie
 * @since 2024-05-23
 */
@Service
public class VersionServiceImpl extends ServiceImpl<VersionMapper, Version> implements IVersionService {

    @Autowired
    IFileMapService iFileMapService;

    @Override
    public R releaseVersion(ReleaseVersionDTO releaseVersion, MultipartFile file) throws IOException {
        Version version = new Version();
        if(file != null){
            String[] parts = {"version"};
            String fileID = iFileMapService.UpdateFileMap(file, parts);
            String filePath = iFileMapService.getFilePathByID(fileID);
            version.setVersionUrl("http://8.134.139.46:8081" + filePath);
        }
        if(version.getVersionUrl() != null && !version.getVersionUrl().isEmpty()){
            version.setVersionName(releaseVersion.getVersionName());
            version.setVersionNumber(releaseVersion.getVersionNumber());
            version.setVersionIntroduce(releaseVersion.getVersionIntroduce());
            version.setCreationTime(LocalDateTime.now());
            int insert = baseMapper.insert(version);
            if (insert > 0) {
                return R.succeed("版本发布成功");
            }
        }
        return R.failed("版本发布失败");
    }

    @Override
    public ListForm<VersionVO> getVersionsList(VersionDTO versionDTO) {
        LambdaQueryWrapper<Version> wrapper = new LambdaQueryWrapper<>();
        if(versionDTO.getStartTime() != null && versionDTO.getEndTime() != null) {
            // 如果开始时间和结束时间都存在，查询这个时间范围内的版本
            wrapper.between(Version::getCreationTime, versionDTO.getStartTime(), versionDTO.getEndTime());
        } else if (versionDTO.getStartTime() != null) {
            // 如果只有开始时间，查询这一天的版本
            LocalDateTime startOfDay = versionDTO.getStartTime().withHour(0).withMinute(0).withSecond(0).withNano(0);
            LocalDateTime endOfDay = versionDTO.getStartTime().withHour(23).withMinute(59).withSecond(59).withNano(999999999);
            wrapper.between(Version::getCreationTime, startOfDay, endOfDay);
        } else if (versionDTO.getEndTime() != null) {
            // 如果只有结束时间，查询这一天的版本
            LocalDateTime startOfDay = versionDTO.getEndTime().withHour(0).withMinute(0).withSecond(0).withNano(0);
            LocalDateTime endOfDay = versionDTO.getEndTime().withHour(23).withMinute(59).withSecond(59).withNano(999999999);
            wrapper.between(Version::getCreationTime, startOfDay, endOfDay);
        }
        // 按照创建时间倒序排列
        wrapper.orderByDesc(Version::getCreationTime);
        Page<Version> versionPage = new Page<>(versionDTO.getPage(), versionDTO.getSize());
        IPage<Version> versionIPage = page(versionPage, wrapper);
        List<VersionVO> versionVOList = versionIPage.getRecords().stream().map(version -> {
            VersionVO versionVO = new VersionVO();
            BeanUtil.copyProperties(version, versionVO);
            return versionVO;
        }).toList();
        return new ListForm<>(versionDTO.getPage(),versionDTO.getSize(),
                versionIPage.getPages(), //总页数
                versionIPage.getTotal(), //总记录数total
                versionVOList);
    }

    @Override
    public List<String> getVersionNumbers() {
        List<Version> versions = list();
        // 获取所有的版本号并返回
        return versions.stream().map(Version::getVersionNumber).collect(Collectors.toList());
    }
}
